<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'othershou.add'" @click="add"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'othershou.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
	            <!--导出-->
	            <sc-export-button :auth="'othershou.export'" :apiObj="apiObj" :search="search" :params="query"></sc-export-button>
	            <!-- 打印-->
	            <sc-print-button :auth="'othershou.print'" :selection="selection"></sc-print-button>
                <div class="finish">
                    <div class="finishNum">{{$t('finance.allnumber')}}:<div class="text">{{total_stock.number}}</div></div>
                    <div class="finishNum">{{$t('finance.allmoney')}}:<div class="text">{{total_stock.money}}</div></div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.other_code" :placeholder="$t('all.code')" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.customer_id" filterable clearable :placeholder="$t('all.select')">
                        <el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"/>
                    </el-select>
                    <el-select v-model="search.supplier_id" :placeholder="$t('procure.apply.apply')" filterable clearable>
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="query" @setExtend="setExtend">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" :label="$t('all.sort')" fixed align="center" width="60"></el-table-column>
                <el-table-column :label="$t('all.code')" prop="other_code" align="center"></el-table-column>
                <el-table-column :label="$t('sale.sale.date')" prop="other_date" align="center"></el-table-column>
                <el-table-column :label="$t('finance.payunit')" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.supplier_id != 0">{{scope.row.supplier.apply_name}}</span>
                        <span v-else>{{scope.row.customer.customer_name}}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('finance.incometype')" prop="type.name" align="center"></el-table-column>
                <!-- <el-table-column :label="$t('finance.payunit')" prop="supplier.mobile" align="center"></el-table-column> -->
                <el-table-column :label="$t('finance.account')" prop="settlement.account" align="center"></el-table-column>
                <el-table-column :label="$t('procure.order.paymoney')" prop="money" align="center"></el-table-column>
                <!-- <el-table-column label="制单人" prop="all_pay_money" align="center"></el-table-column> -->
                <el-table-column :label="$t('all.checkstatus')" prop="status" align="center">
                    <template #default="scope">
                        <!-- <el-tag :type="scope.row.status == 0 ? 'warning' : scope.row.status == 1 ? 'success' : 'danger'">
                            {{scope.row.status == 0 ? '待审核' : scope.row.status == 1 ? '审核通过' : '审核拒绝'}}
                        </el-tag> -->
                        <!-- <el-tag :type="scope.row.status == 0 ? 'warning' :  'success'">
                            {{scope.row.status == 0 ? '待审核' : '已审核'}}
                        </el-tag> -->
                        <el-tag type="success" v-if="scope.row.status==1">{{$t('all.checking')}}</el-tag>
                        <el-tag type="warning" v-else>{{$t('all.awit')}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('all.remark')" prop="remark" align="center"></el-table-column>
                <el-table-column :label="$t('all.operate')" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'othershou.show'">{{$t('all.show')}}</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'othershou.edit'" v-if="scope.row.status == 0">{{$t('all.edit')}}</el-button>
                            <el-popconfirm :title="$t('all.suredel')" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'othershou.delete'" v-if="scope.row.status == 0">{{$t('all.delete')}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button text type="warning" size="small" @click="table_check(scope.row, scope.$index)" v-auth="'othershou.check'" v-if="scope.row.status == 0">{{$t('all.check')}}</el-button>
                            <el-popconfirm title="确定反审核吗?" @confirm="table_uncheck(scope.row)">
								<template #reference>
									<el-button text type="warning" size="small" v-auth="'otherout.uncheck'" v-if="scope.row.status == 1">{{$t('all.uncheck')}}</el-button>
								</template>
							</el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>

            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false"></check-status>
</template>

<script>
import CheckStatus from '../checkStatus.vue'
import saveDialog from './save'

export default {
	name: '客户列表',
	components: {
		saveDialog,CheckStatus,
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
			},
			apiObj: this.$API.finance.sz.list,
			selection: [],
			search: {
				other_code: null,
                customer_id:null,
                supplier_id:null,
                other_type:1
			},
            query:{
                other_type:1
            },
            clientList: [],
            applyList: [],
            total_stock: {},
		}
	},
    mounted () {
        this.getClient();
        this.getApply()
    },
	methods: {
        setExtend(data){
            this.total_stock = data
        },
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
        //审核
		table_check(row) {
			this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('income').setData(row)
			})
		},
        // 反审核
        async table_uncheck(row){
            var reqData = {id: row.id}
            var res = await this.$API.finance.sz.uncheck.post(reqData);
            if(res.code == 1){
                this.$refs.table.refresh()
                this.$message.success(this.$t('all.yes'))
            }else{
                this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
            }
        },
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.finance.sz.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { id: delete_ids }
			var res = await this.$API.finance.sz.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`${this.$t('all.longmessage')} ${this.selection.length} ${this.$t('all.term')}`, this.$t('all.prompt'), {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'income') {
				this.$refs.table.refresh()
			}
		},
        // 客户
        async getClient(){
            var res = await this.$API.client.client.all.get();
            this.clientList = res.data;
        },
        // 供应商
        async getApply(){
            var res = await this.$API.apply.apply.all.get();
            this.applyList = res.data;
        },
	}
}
</script>

<style></style>
